抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

这段时间碰到了一个需求。用户上传了很多的文件希望在SAP GUI中预览其中的PDF内容。然后在网上查阅了一些资料,发现了SAP中CL_GUI_HTML_VIEWER类的实例方法 LOAD_DATA。该类是一个HTML控件类,和OOALV中的ALV GRID控件类似,需要绑定容器使用。简单的理解就是在容器中展示一个网页或HTML元素,然后在这个网页中展示PDF文件。

SAP-GUI中显示PDF文件或网页

一、展示PDF文件

1.效果图展示

1.SAP GUI中展示PDF文件效果图

2.实现过程

(1)定义容器与组件对象

1
2
3
DATA:
GO_HTML_CONTROL TYPE REF TO CL_GUI_HTML_VIEWER, "组件对象
GO_CONTAINER TYPE REF TO CL_GUI_DOCKING_CONTAINER. "容器对象

(2)获取文件的二进制流

实现PDF文件展示在SAP GUI中的思路是先将文件转换为二进制的形式,然后再通过网页展示文件内容。所以这里主要的工作是将文件转换为二进制,这样的方式有很多。你可以通过文件服务器直接将文件的二进制数据放在内表中,也可以使用SAP标准函数GUI_UPLOAD将本地文件转换为二进制形式。

本篇笔记采用第二种使用GUI_UPLOAD的方式获取本地文件的二进制流数据。

① 获取文件路径——选择屏幕
1
2
PARAMETERS:
P_FPATH TYPE RLGRAP-FILENAME MODIF ID FP. "文件上传路径
② 获取文件路径——搜索帮助
1
2
3
4
5
6
7
8
9
10
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FPATH.
PERFORM SEARCH_HELP_FILEPATH CHANGING P_FPATH.

FORM SEARCH_HELP_FILEPATH CHANGING VALUE(PV_FILEPATH) TYPE RLGRAP-FILENAME.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
FIELD_NAME = 'C:\'
IMPORTING
FILE_NAME = PV_FILEPATH.
ENDFORM.
③ 获取文件的二进制流数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
FORM GET_FILE_DATA .
*--------------------------Variables-----------------------------------*
DATA:
LV_FILEPATH TYPE STRING.
*----------------------------Logic-------------------------------------*
CHECK P_DDATA IS NOT INITIAL."选择屏幕的单选按钮,用来代表选中时程序用于展示PDF
REFRESH: GT_BINTAB.
"文件的路径
LV_FILEPATH = P_FPATH.
"上传文件为二进制流数据
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILEPATH
FILETYPE = 'BIN'
* HAS_FIELD_SEPARATOR = ''
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = LV_CODEPAGE
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
NO_AUTH_CHECK = 'X'
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
DATA_TAB = GT_BINTAB "内表类型为 TYPE W3MIMETABTYPE
* CHANGING
* ISSCANPERFORMED = ' '
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.

(3)实例化容器与组件对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FORM CREATE_CONTAINER_ASSEMBLY .
"实例化容器对象
IF GO_CONTAINER IS INITIAL.
CREATE OBJECT GO_CONTAINER
EXPORTING
REPID = SY-REPID
DYNNR = SY-DYNGR
SIDE = CL_GUI_DOCKING_CONTAINER=>DOCK_AT_LEFT
EXTENSION = 2000.
ENDIF.

"实例化组件对象
CREATE OBJECT GO_HTML_CONTROL
EXPORTING
PARENT = GO_CONTAINER.
ENDFORM.

(4)将文件的二进制内容转为网页并展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
FORM SET_ASSEMBLY_DATA .
*--------------------------Variables-----------------------------------*
DATA:
LV_URL TYPE C LENGTH 200.
*----------------------------Logic-------------------------------------*
IF P_DDATA IS NOT INITIAL.
"转换PDF文件
CALL METHOD GO_HTML_CONTROL->LOAD_DATA(
EXPORTING
TYPE = 'application'
SUBTYPE = 'PDF'
IMPORTING
ASSIGNED_URL = LV_URL
CHANGING
DATA_TABLE = GT_BINTAB
EXCEPTIONS
DP_INVALID_PARAMETER = 1
DP_ERROR_GENERAL = 2
CNTL_ERROR = 3
OTHERS = 4 ).
ELSE.
LV_URL = P_WWURL.
ENDIF.

"展示组件内容
CALL METHOD GO_HTML_CONTROL->SHOW_URL( URL = LV_URL
IN_PLACE = 'X' ).
ENDFORM.

二、展示网页内容

该组件的本质还是一个网页组件,所以对于网页能做的事情,他基本都可以做。简单来说,其实就是你在SAP的GUI中插入了一个网页而已。所以无论是你自己手写的HTML代码还是说网址都是可以展示的。

下面就是展示必应官网的过程,实现的逻辑其实比展示PDF还要简单的多。

1.效果图

2.SAP GUI中展示网页信息

2.实现步骤

因为这个的实现逻辑在上面的代码中已经存在了,所以就不再贴代码了。只文字说明下实现的步骤即可。后面会将整个程序的源码贴出来。

  • 创建容器与组件对象
  • 实例化组件与容器对象
  • 调用SHOW_URL Function URL参数传递网页地址,例如必应的官网:HTTP://WWW.BING.COM/?MKT=ZH-CN

三、程序源码与截图

1.主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
REPORT YDISPLAY_PDF.

*----------------------------------------------------------------------*
* Include
*----------------------------------------------------------------------*
INCLUDE YDISPLAY_PDF_TOP.
INCLUDE YDISPLAY_PDF_SCR.
*INCLUDE YDISPLAY_PDF_CLA.
INCLUDE YDISPLAY_PDF_PBO.
INCLUDE YDISPLAY_PDF_PAI.
INCLUDE YDISPLAY_PDF_F01.

*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
"展示和隐藏选择屏幕的输入框
PERFORM SELECT_SCREEN_DISPLAY.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FPATH.
"上传文件地址搜索帮助
PERFORM SEARCH_HELP_FILEPATH CHANGING P_FPATH.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
"将文件转换为二进制流
PERFORM GET_FILE_DATA.

*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.
IF SY-BATCH IS INITIAL.
"展示网页组件的屏幕
CALL SCREEN 0100.
ENDIF.

2.全局变量定义 TOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
*&---------------------------------------------------------------------*
*& Include YDISPLAY_PDF_TOP
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
CONSTANTS:
GC_X TYPE C LENGTH 1 VALUE 'X',
GC_S TYPE C LENGTH 1 VALUE 'S',
GC_E TYPE C LENGTH 1 VALUE 'E',
GC_NULL TYPE C LENGTH 1 VALUE ''.

*----------------------------------------------------------------------*
* Variables
*----------------------------------------------------------------------*
DATA:
GV_MESSAGE TYPE C,
GV_SAVEOK TYPE SY-UCOMM,
GV_OKCODE TYPE SY-UCOMM.

*----------------------------------------------------------------------*
* Structure
*----------------------------------------------------------------------*
DATA:
BEGIN OF GS_TABLE,
FIELD,
END OF GS_TABLE.

*----------------------------------------------------------------------*
* Internal Table
*----------------------------------------------------------------------*
DATA:
GT_TABLE LIKE TABLE OF GS_TABLE,
GT_BINTAB TYPE W3MIMETABTYPE.

*----------------------------------------------------------------------*
* Object
*----------------------------------------------------------------------*
*CLASS LCL_EVENTS DEFINITION DEFERRED.
DATA:
* GO_EVENTS TYPE REF TO LCL_EVENTS,
GO_HTML_CONTROL TYPE REF TO CL_GUI_HTML_VIEWER,
GO_CONTAINER TYPE REF TO CL_GUI_DOCKING_CONTAINER.

3.选择屏幕的定义 SCR

(1)源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
*&---------------------------------------------------------------------*
*& Include YDISPLAY_PDF_SCR
*&---------------------------------------------------------------------*
"程序执行类型
SELECTION-SCREEN BEGIN OF BLOCK K01 WITH FRAME TITLE TEXT-T01.
SELECTION-SCREEN BEGIN OF LINE.
"执行方式
PARAMETERS:
P_TEXT NO-DISPLAY.
SELECTION-SCREEN COMMENT 1(23) TEXT-T03 FOR FIELD P_TEXT.

"展示网页
SELECTION-SCREEN POSITION 32.
PARAMETERS:
P_DISPL RADIOBUTTON GROUP GR01 USER-COMMAND UC1 DEFAULT 'X'.
SELECTION-SCREEN COMMENT 35(15) TEXT-R01 FOR FIELD P_DISPL.

"展示PDF文件内容
SELECTION-SCREEN POSITION 50.
PARAMETERS:
P_DDATA RADIOBUTTON GROUP GR01 .
SELECTION-SCREEN COMMENT 52(15) TEXT-R02 FOR FIELD P_DDATA.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK K01.

"输入内容
SELECTION-SCREEN BEGIN OF BLOCK K02 WITH FRAME TITLE TEXT-T02.
PARAMETERS:
P_FPATH TYPE RLGRAP-FILENAME MODIF ID FP, "文件上传路径
P_WWURL TYPE STRING MODIF ID WU. "网址URL
SELECTION-SCREEN END OF BLOCK K02.

(2)效果图

3.选择屏幕效果图_展示网页 4.选择屏幕效果图_展示PDF文件

4.逻辑部分代码

(1)PBO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'G0100'.
SET TITLEBAR 'T0100'.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module INITIAL_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE INITIAL_0100 OUTPUT.
PERFORM INITIAL_0100.
ENDMODULE.

(2)PAI

1
2
3
4
5
6
7
8
*&---------------------------------------------------------------------*
*& Module EXIT INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE EXIT INPUT.
PERFORM EXIT.
ENDMODULE.

(3)F01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
*&---------------------------------------------------------------------*
*& Include YDISPLAY_PDF_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form EXIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM EXIT .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_BACK TYPE SY-UCOMM VALUE 'BACK',
LC_EXIT TYPE SY-UCOMM VALUE 'EXIT',
LC_CANC TYPE SY-UCOMM VALUE 'CANC'.
*----------------------------Logic-------------------------------------*
CLEAR: GV_SAVEOK.
GV_SAVEOK = GV_OKCODE.
CLEAR: GV_OKCODE.

CASE GV_SAVEOK.
WHEN LC_BACK OR LC_EXIT OR LC_CANC.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INITIAL_0100
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM INITIAL_0100 .
IF GO_HTML_CONTROL IS NOT BOUND.
"创建容器与组件对象
PERFORM CREATE_CONTAINER_ASSEMBLY.
"设置组件展示用的内容
PERFORM SET_ASSEMBLY_DATA.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CREATE_CONTAINER_ASSEMBLY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM CREATE_CONTAINER_ASSEMBLY .
"实例化容器对象
IF GO_CONTAINER IS INITIAL.
CREATE OBJECT GO_CONTAINER
EXPORTING
REPID = SY-REPID
DYNNR = SY-DYNGR
SIDE = CL_GUI_DOCKING_CONTAINER=>DOCK_AT_LEFT
EXTENSION = 2000.
ENDIF.

"实例化组件对象
CREATE OBJECT GO_HTML_CONTROL
EXPORTING
PARENT = GO_CONTAINER.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_ASSEMBLY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SET_ASSEMBLY_DATA .
*--------------------------Variables-----------------------------------*
DATA:
LV_URL TYPE C LENGTH 200.
*----------------------------Logic-------------------------------------*
"展示PDF文件
IF P_DDATA IS NOT INITIAL.
CALL METHOD GO_HTML_CONTROL->LOAD_DATA(
EXPORTING
TYPE = 'application'
SUBTYPE = 'PDF'
IMPORTING
ASSIGNED_URL = LV_URL
CHANGING
DATA_TABLE = GT_BINTAB
EXCEPTIONS
DP_INVALID_PARAMETER = 1
DP_ERROR_GENERAL = 2
CNTL_ERROR = 3
OTHERS = 4 ).
ELSE.
LV_URL = P_WWURL.
ENDIF.

"展示组件内容
CALL METHOD GO_HTML_CONTROL->SHOW_URL( URL = LV_URL
IN_PLACE = 'X' ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SELECT_SCREEN_DISPLAY
*&---------------------------------------------------------------------*
*& text 根据选择屏幕单选按钮控制屏幕元素展示内容
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SELECT_SCREEN_DISPLAY .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_DISPL TYPE STRING VALUE 'FP',
LC_DDATA TYPE STRING VALUE 'WU'.
DATA:
LV_GROUP TYPE STRING.
*----------------------------Logic-------------------------------------*
CASE GC_X.
WHEN P_DISPL.
"展示网页
LV_GROUP = LC_DISPL.
WHEN P_DDATA.
"展示PDF文件内容
LV_GROUP = LC_DDATA.
WHEN OTHERS.
ENDCASE.
"隐藏屏幕元素
LOOP AT SCREEN.
IF SCREEN-GROUP1 EQ LV_GROUP.
SCREEN-ACTIVE = 0.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_FILE_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM GET_FILE_DATA .
*--------------------------Variables-----------------------------------*
DATA:
LV_FILEPATH TYPE STRING.
*----------------------------Logic-------------------------------------*
CHECK P_DDATA IS NOT INITIAL.
REFRESH: GT_BINTAB.

LV_FILEPATH = P_FPATH.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILEPATH
FILETYPE = 'BIN'
* HAS_FIELD_SEPARATOR = ''
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = LV_CODEPAGE
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
NO_AUTH_CHECK = 'X'
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
DATA_TAB = GT_BINTAB
* CHANGING
* ISSCANPERFORMED = ' '
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form SEARCH_HELP_FILEPATH
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- P_FPATH
*&---------------------------------------------------------------------*
FORM SEARCH_HELP_FILEPATH CHANGING VALUE(PV_FILEPATH) TYPE RLGRAP-FILENAME.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
FIELD_NAME = 'C:\'
IMPORTING
FILE_NAME = PV_FILEPATH.
ENDFORM.

评论